<template>
  <div class="app-container">
    <div class="" style="width:100%;padding:10px;height:50px">
      <el-button size="mini" type="primary" style="float:left" @click="handleCreate()">新增</el-button>
      <el-button
        size="mini"
        type="primary"
        style="float: right; margin-left: 10px"
        icon="el-icon-search"
        @click="handleFilter"
        >搜索
      </el-button>
      <el-input
            v-model="listQuery.systemName"
            autocomplete="off"
            placeholder="请输入系统名称"
        style="float: right; width: 150px;margin-left: 5px;"
          />
      <el-select
        v-model="listQuery.systemId"
        filterable
        placeholder="请选择系统"
        style="float: right; width: 150px"
        @change="getPage()"
      >
        <el-option
          v-for="item in systemOptions"
          :key="item.systemId"
          :label="item.systemName"
          :value="item.systemId"
        >
        </el-option>
      </el-select>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :header-cell-style="{'background-color': '#F5F8FB'}"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" label="序号" type="index" :index="indexMethod" />
      <!--<el-table-column align="center" label="企业ID">
        <template slot-scope="scope">
          <span :title="scope.row.businessId">{{ scope.row.businessId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="系统id">
        <template slot-scope="scope">
          <span :title="scope.row.systemId">{{ scope.row.systemId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="权限组ID">
        <template slot-scope="scope">
          <span :title="scope.row.groupId">{{ scope.row.groupId }}</span>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="权限组名称">
        <template slot-scope="scope">
          <span :title="scope.row.groupName">{{ scope.row.groupName }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="创建用户ID">
        <template slot-scope="scope">
          <span :title="scope.row.userId">{{ scope.row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="类型">
        <template slot-scope="scope">
          <span :title="scope.row.type">{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <span :title="scope.row.status">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建人">
        <template slot-scope="scope">
          <span :title="scope.row.createId">{{ scope.row.createId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span :title="scope.row.createTime">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新人">
        <template slot-scope="scope">
          <span :title="scope.row.updateId">{{ scope.row.updateId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间">
        <template slot-scope="scope">
          <span :title="scope.row.updateTime">{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="删除状态(0-正常,1-已删除)">
        <template slot-scope="scope">
          <span :title="scope.row.delFlag">{{ scope.row.delFlag }}</span>
        </template>
      </el-table-column>-->
      <el-table-column fixed="right" align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="info" @click="handleAuth(scope.row)">授权</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="权限组维护" :visible.sync="dialogVisible">
      <el-form ref="groupForm" :model="group" :rules="rules" size="mini">
        <!--<el-form-item label="企业ID" prop="businessId" :label-width="formLabelWidth">
          <el-input v-model="group.businessId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="系统id" prop="systemId" :label-width="formLabelWidth">
          <el-input v-model="group.systemId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="权限组ID" prop="groupId" :label-width="formLabelWidth">
          <el-input v-model="group.groupId" autocomplete="off" />
        </el-form-item>-->
        <el-form-item label="权限组名称" prop="groupName" :label-width="formLabelWidth">
          <el-input v-model="group.groupName" autocomplete="off" />
        </el-form-item>
        <!--<el-form-item label="创建用户ID" prop="userId" :label-width="formLabelWidth">
          <el-input v-model="group.userId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" prop="type" :label-width="formLabelWidth">
          <el-input v-model="group.type" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态" prop="status" :label-width="formLabelWidth">
          <el-input v-model="group.status" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创建人" prop="createId" :label-width="formLabelWidth">
          <el-input v-model="group.createId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime" :label-width="formLabelWidth">
          <el-input v-model="group.createTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateId" :label-width="formLabelWidth">
          <el-input v-model="group.updateId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime" :label-width="formLabelWidth">
          <el-input v-model="group.updateTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="删除状态(0-正常,1-已删除)" prop="delFlag" :label-width="formLabelWidth">
          <el-input v-model="group.delFlag" autocomplete="off" />
        </el-form-item>-->
        <el-form-item label="菜单授权" prop="parentId" :label-width="formLabelWidth">
          <el-tree :data="tree"
            :props="defaultProps"
            v-model ="group.menuIds"
            node-key="menuId"
            show-checkbox
            @check-change="handleCheckChange"
            :default-checked-keys="treeListChecked"
            ref="tree"
            >
          </el-tree>
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSaveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="授权管理" :visible.sync="authVisible">
      <el-form ref="authForm" size="mini">
        <el-checkbox-group v-model="checkList">
          <el-checkbox v-for="(user, index) in userList" :label="user.userId" :key="user.userId">{{user.userName }}({{user.phone}}/{{user.email}})</el-checkbox>
          <!--<div v-for="(system, index) in userList">
            <h3 style="font-size: 16px;">{{ system.systemName }}</h3>
            <el-checkbox v-for="(group, index) in system.group" :label="group.groupId" :key="group.groupId">{{group.groupName }}</el-checkbox>
          </div>-->
        </el-checkbox-group>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="authVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSaveGroup()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { del, get, getPage, save, update } from '@/api/Group'

import { getAll } from '@/api/System'

import { getTree } from '@/api/Menu'

import { getUser } from '@/api/User'

import { saveGroup } from '@/api/Relation'

export default {
  data() {
    return {
      total: null,
      list: null,
      listLoading: true,
      dialogVisible: false,
      authVisible: false,
      listQuery: {
        page: 1,
        size: 10,
        systemId: null
      },
      tree: [],
      treeListChecked: [],
      userList:[],
      checkList:[],
      defaultProps:{
                children: 'childList',
                label: 'menuName'
            },
      systemOptions:[],
      group: {
        businessId: null,
        systemId: null,
        groupId: null,
        groupName: null,
        userId: null,
        type: null,
        status: null,
        createId: null,
        createTime: null,
        updateId: null,
        updateTime: null,
        delFlag: null,
        menuIds: [],
        menuId: null
      },
      groupCopy: null,
      rules: {
        businessId: [{ required: true, message: '请填写企业ID', trigger: 'blur' }],
        systemId: [{ required: true, message: '请填写系统id', trigger: 'blur' }],
        groupId: [{ required: true, message: '请填写权限组ID', trigger: 'blur' }],
        groupName: [{ required: true, message: '请填写权限组名称', trigger: 'blur' }],
        userId: [{ required: true, message: '请填写创建用户ID', trigger: 'blur' }],
        type: [{ required: true, message: '请填写类型', trigger: 'blur' }],
        status: [{ required: true, message: '请填写状态', trigger: 'blur' }],
        createId: [{ required: true, message: '请填写创建人', trigger: 'blur' }],
        createTime: [{ required: true, message: '请填写创建时间', trigger: 'blur' }],
        updateId: [{ required: true, message: '请填写更新人', trigger: 'blur' }],
        updateTime: [{ required: true, message: '请填写更新时间', trigger: 'blur' }],
        delFlag: [{ required: true, message: '请填写删除状态(0-正常,1-已删除)', trigger: 'blur' }],
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getSystem()
  },
  methods: {
    getSystem() {
      getAll().then(response => {
        this.systemOptions = response.data
        if(response.data.length > 0){
          this.listQuery.systemId = response.data[0].systemId
          this.getPage();
        }
      })
    },
    handleCheckChange(data, checked, indeterminate) {

this.tCategory.parentId = data.id;
console.log(data, checked, indeterminate);
},
    getPage() {
      this.listLoading = true
      getTree({systemId:this.listQuery.systemId}).then(response => {
        this.tree = response.data
      })
      getPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getPage()
    },
    indexMethod(index) {
      return this.listQuery.size * (this.listQuery.page - 1) + index + 1
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getPage()
    },
    handleCreate() {
      Object.keys(this.group).forEach(key => (this.group[key] = null))
      this.groupCopy = null
      this.dialogVisible = true
      this.treeListChecked=[];
      if (this.$refs['groupForm'] !== undefined) {
        this.$refs['groupForm'].clearValidate()
      }
    },
    handleEdit(row) {
      const params = { id: row.groupId }
      this.treeListChecked=[];
      get(params).then(response => {
        this.group = response.data
        this.treeListChecked = response.data.menuIds
        this.groupCopy = response.data
      })
      this.dialogVisible = true
      if (this.$refs['groupForm'] !== undefined) {
        this.$refs['groupForm'].clearValidate()
      }
    },
    handleAuth(row) {
      const params = { id: row.groupId }
      get(params).then(response => {
        this.group = response.data
        this.groupCopy = response.data
      })
      getUser(row.groupId).then(response => {
        this.userList = response.data.user
        this.checkList = response.data.check
      })
      this.authVisible = true;
    },
    handleDelete(row) {
      this.$confirm('是否删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.groupId).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    handleSaveOrUpdate() {

      let checkedData = this.$refs.tree.getCheckedKeys();
      this.group.menuIds = checkedData
      this.$refs['groupForm'].validate((valid) => {
        if (valid) {
          this.group.systemId = this.listQuery.systemId
          if (this.group.groupId) {
            update(this.group).then(() => {
              this.$message({
                type: 'success',
                message: '更新成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          } else {
            save(this.group).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          }
        } else {
          this.$message.error('请保证数据填写正确！')
        }
      })
    },
    handleSaveGroup(){
          var system = {businessId:this.group.businessId,systemId:this.group.systemId,groupId:this.group.groupId,checkList:this.checkList}
          saveGroup(system).then(() => {
            this.$message({
              type: 'success',
              message: '保存成功'
            })
            this.authVisible = false
            this.getPage()
          })
    }
  }
}
</script>

<style>
</style>
